28. Coding the Motion Model
Now that we have manually calculated each step for determining the motion model probability, we will implement these steps in a function. The starter code below steps through each position x, calls the motion_model function and prints the results to stdout. To complete this exercise fill in the motion_model function which will involve:
For each x_{t}. :
- Calculate the transition probability for each potential value x_{t-1}
- Calculate the discrete motion model probability by multiplying the transition model probability by the belief state (prior) for x_{t-1}
Return total probability (sum) of each discrete probability
Start Quiz:
#include <iostream>
#include <algorithm>
#include <vector>
#include "helpers.h"
using namespace std;
std::vector<float> initialize_priors(int map_size, std::vector<float> landmark_positions,
float position_stdev);
float motion_model(float pseudo_position, float movement, std::vector<float> priors,
int map_size, int control_stdev);
int main() {
//set standard deviation of control:
float control_stdev = 1.0f;
//set standard deviation of position:
float position_stdev = 1.0f;
//meters vehicle moves per time step
float movement_per_timestep = 1.0f;
//number of x positions on map
int map_size = 25;
//initialize landmarks
std::vector<float> landmark_positions {5, 10, 20};
// initialize priors
std::vector<float> priors = initialize_priors(map_size, landmark_positions,
position_stdev);
//step through each pseudo position x (i)
for (unsigned int i = 0; i < map_size; ++i) {
float pseudo_position = float(i);
//get the motion model probability for each x position
float motion_prob = motion_model(pseudo_position, movement_per_timestep,
priors, map_size, control_stdev);
//print to stdout
std::cout << pseudo_position << "\t" << motion_prob << endl;
}
return 0;
};
//TODO, implement the motion model: calculates prob of being at an estimated position at time t
float motion_model(float pseudo_position, float movement, std::vector<float> priors,
int map_size, int control_stdev) {
//initialize probability
float position_prob = 0.0f;
//YOUR CODE HERE
return position_prob;
}
//initialize priors assumimg vehicle at landmark +/- 1.0 meters position stdev
std::vector<float> initialize_priors(int map_size, std::vector<float> landmark_positions,
float position_stdev) {
//initialize priors assumimg vehicle at landmark +/- 1.0 meters position stdev
//set all priors to 0.0
std::vector<float> priors(map_size, 0.0);
//set each landmark positon +/1 to 1.0/9.0 (9 possible postions)
float normalization_term = landmark_positions.size() * (position_stdev * 2 + 1);
for (unsigned int i = 0; i < landmark_positions.size(); i++){
int landmark_center = landmark_positions[i];
priors[landmark_center] = 1.0f/normalization_term;
priors[landmark_center - 1] = 1.0f/normalization_term;
priors[landmark_center + 1] = 1.0f/normalization_term;
}
return priors;
}
//=================================================================================
// Name : help_functions.h
// Version : 2.0.0
// Copyright : Udacity
//=================================================================================
#ifndef HELP_FUNCTIONS_H_
#define HELP_FUNCTIONS_H_
#include <math.h>
#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <iomanip>
using namespace std;
class Helpers {
public:
//definition of one over square root of 2*pi:
constexpr static float STATIC_ONE_OVER_SQRT_2PI = 1/sqrt(2*M_PI) ;
float ONE_OVER_SQRT_2PI = 1/sqrt(2*M_PI) ;
/*****************************************************************************
* normpdf(X,mu,sigma) computes the probability function at values x using the
* normal distribution with mean mu and standard deviation std. x, mue and
* sigma must be scalar! The parameter std must be positive.
* The normal pdf is y=f(x;mu,std)= 1/(std*sqrt(2pi)) e[ -(x−mu)^2 / 2*std^2 ]
*****************************************************************************/
static float normpdf(float x, float mu, float std) {
return (STATIC_ONE_OVER_SQRT_2PI/std)*exp(-0.5*pow((x-mu)/std,2));
}
};
#endif /* HELP_FUNCTIONS_H_ */